package org.flexharmony.eclipse.properties.classinclusion;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.flexharmony.eclipse.HarmonyPlugin;

public class AddSinglePatternDialog extends Dialog
{
  private String initPattern;
  
  private IFolder sourceFolder;
  
  private boolean include;

  private boolean add;
  
  private String pattern;
  
  private Text patternText;

  private Button okButton;
  
  public AddSinglePatternDialog(Shell parentShell, String initPattern, IFolder sourceFolder, boolean include)
  {
    super(parentShell);
    this.initPattern = initPattern;
    this.sourceFolder = sourceFolder;
    this.include = include;
  }

  public boolean isAdd()
  {
    return add;
  }

  public String getPattern()
  {
    return pattern;
  }

  protected void configureShell(Shell shell)
  {
    super.configureShell(shell);
    shell.setText(include ? "Inclusion Patterns" : "Exclusion Patterns");
  }
  
  protected Control createDialogArea(Composite parent)
  {
    Composite composite = (Composite) super.createDialogArea(parent);
    
    GridData gd = new GridData();
    gd.widthHint = 500;
    gd.heightHint = 120;
    composite.setLayoutData(gd);
    
    Composite dialogPanel = new Composite(composite, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    dialogPanel.setLayout(layout);
    gd = new GridData();
    gd.grabExcessHorizontalSpace = true;
    gd.grabExcessVerticalSpace = true;
    gd.verticalAlignment = GridData.FILL;
    gd.horizontalAlignment = GridData.FILL;
    dialogPanel.setLayoutData(gd);
    
    
    Label text = new Label(dialogPanel, SWT.WRAP);
    gd = new GridData();
    gd.horizontalSpan = 2;
    gd.horizontalAlignment = GridData.FILL;
    text.setLayoutData(gd);
    
    String verb = include ? "including" : "excluding";
//    text.setText("Test");
    text.setText(String.format("Enter a pattern for %s files to be harmonized into ActionScript.\nAll Java style regular expression are valid.", verb));
    
    Label label = new Label(dialogPanel, SWT.NONE);
    gd = new GridData();
    gd.horizontalSpan = 2;
    label.setLayoutData(gd);
    String title = include ? "Inclusion" : "Exclusion";
    label.setText(String.format("%s pattern (Path relative to %s):", title, sourceFolder.getFullPath().toString()));
    
    patternText = new Text(dialogPanel, SWT.SINGLE | SWT.BORDER);
    if (initPattern != null) patternText.setText(initPattern);
    patternText.addKeyListener(new KeyAdapter()
    {
      public void keyReleased(KeyEvent evt) 
      {
        okButton.setEnabled(patternText.getText().trim().length() > 0);
      }      
    });
    gd = new GridData();
    gd.grabExcessHorizontalSpace = true;
    gd.horizontalAlignment = GridData.FILL;
    gd.horizontalSpan = 1;
    patternText.setLayoutData(gd);
    
    Button browseButton = new Button(dialogPanel, SWT.NONE);
    browseButton.setText("Browse...");
    browseButton.addSelectionListener(new SelectionAdapter()
    {
      public void widgetSelected(SelectionEvent sevt)
      {
        browse();
      }
    });    
    return composite;
  }

  protected void createButtonsForButtonBar(Composite parent)
  {
    okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
    okButton.setEnabled(false);
    createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
  }

  protected void buttonPressed(int buttonId) 
  {
    switch (buttonId)
    {
      case IDialogConstants.OK_ID:
        pattern = patternText.getText().trim();
        if (pattern.length() > 0)
        {
          add = true;
        }
        break;
    }
    super.buttonPressed(buttonId);
  }

  
  private void browse()
  {
    Shell shell = HarmonyPlugin.getInstance().getWorkbench().getDisplay().getActiveShell();
    SelectJavaSourceDialog sourceDialog = new SelectJavaSourceDialog(shell, sourceFolder, false, include);
    sourceDialog.create();
    sourceDialog.open();
    
    if (sourceDialog.isAdd())
    {
      IResource resource = sourceDialog.getSelectedResources().get(0);
      IPath path = resource.getFullPath();
      path = path.removeFirstSegments(path.matchingFirstSegments(sourceFolder.getFullPath()));
      if (resource instanceof IFolder)
      {
        patternText.setText(path.toString() + "/.*");
        okButton.setEnabled(true);
      }
      else if (resource instanceof IFile)
      {
        patternText.setText(path.toString());
        okButton.setEnabled(true);
      }
    }
  }
}
